﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DialoguerCore;
using ProtoBuf;

/// <summary>
/// File saver.
/// Does not save progress yet. - 06/25/14
/// Saves progress. Error: Screenshot does not comply. - 07/03/2014
/// Redirected the datas to another script. May/May not work. - 07/10/2014
/// This script forces internal crashing. Must rework. - 08/05/2014
/// Retweaked Save files. Added more factors to save(Current scene, camera Index, Bool/Float flags) - 08/06/2014
/// Author: Thaddeus Paul F. Brillantes
/// </summary>
public class FileSaver : MonoBehaviour {
	#region Singleton Code
	//Persistent Singleton
	private static FileSaver _instance;
	
	public static FileSaver instance
	{
		get
		{
			if(_instance == null)
			{
				_instance = GameObject.FindObjectOfType<FileSaver>();

				DontDestroyOnLoad(_instance.gameObject);
			}

			return _instance;
		}
	}
	#endregion

	[HideInInspector] public ExoSaveData exoSaveData;
	public ScreenshotCapturer screenshotCapturer;
	public BackLogData backlogData;

	void Awake()
	{
		SerializeUtility.Init( new System.Type[]{ typeof(ExoSaveData)} );
		Debug.Log("adding ExoSaveData successful!");

		#region more Singleton code
		if(_instance == null)
		{
			_instance = this;
			DontDestroyOnLoad(this);
		}
		else
		{
			if(this != _instance)
				Destroy(this.gameObject);
		}
		#endregion
		exoSaveData = new ExoSaveData();
	}

	void Start()
	{
		screenshotCapturer = GameObject.Find("SaveLoadManager").GetComponent<ScreenshotCapturer>();
		backlogData = GameObject.Find("DataManager").GetComponent<BackLogData>();
	}

	void Update()
	{
	
	}

	public void saveTheFile(string sender)
	{
		string extraNum = sender[sender.Length - 1].ToString();
		string dataLocation = Application.persistentDataPath + "/SaveFile" + extraNum;

		exoSaveData = new ExoSaveData();
		//1) Date of Save
		exoSaveData.DateOfSave = System.DateTime.Now.ToShortDateString();
		//2) Time of Save
		exoSaveData.TimeOfSave = System.DateTime.Now.ToShortTimeString();
		//3) Current Chapter(Loaded Level)
		exoSaveData.applicationLevel = Application.loadedLevel;
		//4) Current Camera Orientation
		int lasCam = PlayerPrefs.GetInt("curCam");
		exoSaveData.cameraIndex = lasCam;
		//5) Current Dialogue
		int lastDia = PlayerPrefs.GetInt ("LastDia");
		exoSaveData.currentChapter = lastDia;
		//6) Current Inventory IDs
		List<ExogenesisItem> inventory = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().playerInventory;
		List<int> inventoryIDs = new List<int>();
		for(int i = 0; i < inventory.Count; i++)
			inventoryIDs.Add(inventory[i].ID);
		exoSaveData.InventoryIDs = new List<int>(inventoryIDs);
		//7) Current Profile IDs
		List<ProfileData> profiles = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles;
		List<int> profileIDs = new List<int>();
		for(int i = 0; i < profiles.Count; i++)
			profileIDs.Add(profiles[i].ID);
		exoSaveData.ProfileIDs = new List<int>(profileIDs);
		//8) Current Profile Descriptions
		List<string> profileDescs = new List<string>();
		for(int i = 0; i < profiles.Count; i++)
			profileDescs.Add(profiles[i].description);
		exoSaveData.ProfileDescs = new List<string>(profileDescs);
		//9) Current Boolean Flags triggered
		List<bool> bools = new List<bool>();
		for(int i = 0; i < 102; i++)
		{
			bools.Add(Dialoguer.GetGlobalBoolean(i));
		}
		exoSaveData.boolFlags = new List<bool>(bools);
		//10) Current Float Flags triggered
		List<float> floats = new List<float>();
		for(int i = 0; i < 15; i++)
		{
			floats.Add(Dialoguer.GetGlobalFloat(i));
		}
		exoSaveData.floatFlags = new List<float>(floats);
		//11) Current backlog
		backlogData = GameObject.Find("DialogueManager").GetComponent<BacklogDisplayer>().backLogData;
		exoSaveData.BackLog = backlogData.allLines;

		byte[] ssInBytes;
		ssInBytes = screenshotCapturer.screenShotData.getScreenshotInBytes();

		//creates save file
		SerializeUtility.SaveObject(dataLocation, exoSaveData);
		//creates save file screenshot
		System.IO.File.WriteAllBytes(Application.persistentDataPath + "/SaveFile" + extraNum + "Photo", ssInBytes);
	}

	public void quickSave(){
		string dataLocation = Application.persistentDataPath + "/QuickSaveFile";

		exoSaveData = new ExoSaveData();
		//1) Date of Save
		exoSaveData.DateOfSave = System.DateTime.Now.ToShortDateString();
		//2) Time of Save
		exoSaveData.TimeOfSave = System.DateTime.Now.ToShortTimeString();
		//3) Current Chapter(Loaded Level)
		exoSaveData.applicationLevel = Application.loadedLevel;
		//4) Current Camera Orientation
		int lasCam = PlayerPrefs.GetInt("curCam");
		exoSaveData.cameraIndex = lasCam;
		//5) Current Dialogue
		int lastDia = PlayerPrefs.GetInt ("LastDia");
		exoSaveData.currentChapter = lastDia;
		//6) Current Inventory IDs
		List<ExogenesisItem> inventory = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().playerInventory;
		List<int> inventoryIDs = new List<int>();
		for(int i = 0; i < inventory.Count; i++)
			inventoryIDs.Add(inventory[i].ID);
		exoSaveData.InventoryIDs = new List<int>(inventoryIDs);
		//7) Current Profile IDs
		List<ProfileData> profiles = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>().profiles;
		List<int> profileIDs = new List<int>();
		for(int i = 0; i < profiles.Count; i++)
			profileIDs.Add(profiles[i].ID);
		exoSaveData.ProfileIDs = new List<int>(profileIDs);
		//8) Current Profile Descriptions
		List<string> profileDescs = new List<string>();
		for(int i = 0; i < profiles.Count; i++)
			profileDescs.Add(profiles[i].description);
		exoSaveData.ProfileDescs = new List<string>(profileDescs);
		//9) Current Boolean Flags triggered
		List<bool> bools = new List<bool>();
		for(int i = 0; i < 102; i++)
		{
			bools.Add(Dialoguer.GetGlobalBoolean(i));
		}
		exoSaveData.boolFlags = new List<bool>(bools);
		//10) Current Float Flags triggered
		List<float> floats = new List<float>();
		for(int i = 0; i < 15; i++)
		{
			floats.Add(Dialoguer.GetGlobalFloat(i));
		}
		exoSaveData.floatFlags = new List<float>(floats);
		//11) Current backlog
		backlogData = GameObject.Find("DialogueManager").GetComponent<BacklogDisplayer>().backLogData;
		exoSaveData.BackLog = backlogData.allLines;
		
		//creates save file
		SerializeUtility.SaveObject(dataLocation, exoSaveData);
	}
}